package fields;
import boundaryToMatador.GUI;
import game.Player;

public abstract class Ownable extends Field {
	
	protected int price;
	protected Player owner = new Player();
	public int mortgageValue;
	public boolean isMortgaged;
	
	public Ownable (int number, String name, int price)
	{
		super (number,name);
		this.mortgageValue = price/2;
		this.isMortgaged = false;
		this.price=price;
		owner=null;
	}
	
	public int getPrice() {
		return price;
	}
	
	public abstract int getRent();
	
	public int getMortageValue() {
		return this.mortgageValue;
	}
	
	public int getBuyOutFromMortagePrice() {
		return (int) (this.mortgageValue*1.1);
	}
	
	public boolean getIsMortaged() {
		return isMortgaged;
	}
	
	public void buyOutFromMortage() {
		this.isMortgaged = false;
	}
	
	public void mortgage() {
		this.isMortgaged = true;
	}
	
	public void landOnField(Player p)
	{
		if (!isNotOwned())
		{
			p.transfer(getRent(), owner);
		}
		else {
			if (p.getChoice()==1) buy(p);
		}
	}
	
	public boolean isNotOwned()
	{
		if (owner==null) return true;
		else return false;
	}
	
	public void setOwner(Player s)
	{
		owner=s;
	}
	
	public boolean isOwner(Player p)
	{
		if (this.isNotOwned()) {
			return false;
			}
		if (getOwner().compareTo(p.getName())==0) return true;
		else return false;
	}
	
	public String getOwner()
	{
		return owner.getName();
	}
	
	public Player getOwnerObject() {
					return owner;
		}
	
	public void buy(Player s)
	{
		if (isNotOwned()) {
			s.withdraw(price);
			setOwner(s);
		}
	}
	
	public void forSale()
	{
		this.isMortgaged = false;
		owner=null;
		GUI.removeOwner(this.fieldNumber+1);
	}
	

}
